package zj.pojo;

import android.content.ContentValues;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by wjw on 2017/5/28.
 */

public class ZjObject {
    public JSONObject toJsonObject(){
        ContentValues contentValues = this.toContentValues();
        JSONObject jsonObject = new JSONObject();
        for (Map.Entry<String,Object> item : contentValues.valueSet()){
            String key = item.getKey();
            try {
                jsonObject.put(key,item.getValue());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

    public String toJsonString(){
        return this.toJsonObject().toString();
    }

    public void JsonObjectToObject(JSONObject jsonObject){
        ContentValues contentValues = this.toContentValues();
        Iterator iterator = jsonObject.keys();
        while (iterator.hasNext()){
            String key = iterator.next().toString();
            String value = "";
            try {
                value = jsonObject.get(key).toString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if(key.equals("pm2.5")){
                key = "pm25";
            }
            contentValues.put(key,value);
        }
        this.contentValuesToObject(contentValues);
    }

    public ContentValues toContentValues(){
        ContentValues contentValues = new ContentValues();
        Class<?> objectClass = this.getClass();
        Field[] fields = objectClass.getDeclaredFields();
        for (Field field : fields){
            String fieldName = field.getName();
            String getMethodName = "get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
            String fieldTypeName = field.getType().getName();
            try {
                Method getMethod = objectClass.getMethod(getMethodName,new Class[]{});
                if (fieldTypeName.equals("int")){
                    int value = (int) getMethod.invoke(this,new Object[]{});
                    contentValues.put(fieldName,value);
                }else {
                    String value = getMethod.invoke(this,new Object[]{}).toString();
                    if (value!=null && !value.equals("")){
                        contentValues.put(fieldName,value);
                    }
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return contentValues;
    }

    public void contentValuesToObject(ContentValues contentValues){
        Class<?> objectClass = this.getClass();
        Field[] fields = objectClass.getDeclaredFields();
        for (Field field : fields){
            String fieldName = field.getName();
            String setMethodName = "set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
            Class<?> fieldType = field.getType();
            String fieldTypeName = fieldType.getName();
            try {
                Method setMethod = objectClass.getMethod(setMethodName,new Class[]{fieldType});
                if (fieldTypeName.equals("int")){
                    int value = contentValues.getAsInteger(fieldName);
                    setMethod.invoke(this,new Object[]{value});
                }else {
                    String value = contentValues.getAsString(fieldName);
                    setMethod.invoke(this,new Object[]{value});
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static String toCreateSQLString(String className){
        String sql = "";
        try {
            Class<?> objectClass = Class.forName(className);
            className = objectClass.getSimpleName();
            String values = "";
            Field[] fields = objectClass.getDeclaredFields();
            for (Field field : fields){
                String fieldName = field.getName();
                String fieldTypeName = field.getType().getName();
                if (!field.isSynthetic() && !fieldName.equals("uuid")){
                    if (fieldTypeName.equals("int")){
                        values += String.format(",%s %s",fieldName,"int");
                    }else {
                        values += String.format(",%s %s",fieldName,"text");
                    }
                }
            }
            sql = String.format("create table %s(uuid text primary key%s)",className,values);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return sql;
    }

    public String getClassName(){
        return this.getClass().getSimpleName();
    }

    public static String toDropSQLString(String className){
        String sql = "drop table ";
        try {
            Class<?> objectClass = Class.forName(className);
            sql += objectClass.getSimpleName();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return sql;
    }
}
